package com.jh.concurrent.threadpool;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author JH_Wong
 * @projectName newforward
 * @title CallableDemo.java
 * @package com.jh.concurrent.threadpool
 * @description Callable例子
 * @date 2019-05-28 11:16
 */
public class CallableDemo implements Callable<String> {

    @Override
    public String call() throws Exception {
        System.out.println("----- call -----");
        TimeUnit.SECONDS.sleep(5);
        return "Hello Call";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableDemo demo = new CallableDemo();

        // FutureTask执行
        /*FutureTask<String> task = new FutureTask<String>(base);
        new Thread(task).start();*/

        // 线程池执行
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        Future<String> task = executorService.submit(demo);
        executorService.shutdown();

        System.out.println(task.get());
    }
}
